package com.example.hibernate.view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.event.ListSelectionListener;

import com.example.hibernate.model.entity.Department;
import com.example.hibernate.model.entity.Employee;
import com.example.hibernate.view.util.DepartmentComboBoxModel;
import com.example.hibernate.view.util.EmployeeTableModel;
import com.example.hibernate.view.util.WidgetFactory;

public class MainFrame {

	static {
		LookAndFeelInfo[] lookAndFeels = UIManager.getInstalledLookAndFeels();
		try {
			UIManager.setLookAndFeel(lookAndFeels[1].getClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private JFrame frmMain;
	private EmployeeTableModel tableModel;
	private DepartmentComboBoxModel comboBoxModel;
	private JTable table;
	private JTextField txtFirstName;
	private JTextField txtLastName;
	private JTextField txtHireDate;
	private JComboBox<Department> cmbDepartments;

	public MainFrame() {
		initializeComponents();
	}

	private void initializeComponents() {
		frmMain = new JFrame("Hibernate Database Example");
		frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmMain.setSize(800, 600);
		frmMain.setLocationRelativeTo(null);
		{
			JPanel pnlCenter = new JPanel();
			{
				BoxLayout boxLayout = new BoxLayout(pnlCenter, BoxLayout.Y_AXIS);
				pnlCenter.setLayout(boxLayout);
				{
					{
						JPanel pnlUpper = new JPanel();
						pnlUpper.setLayout(new BorderLayout());
						pnlUpper.setBorder(BorderFactory.createTitledBorder("Employees"));
						{
							{
								tableModel = new EmployeeTableModel();
								table = new JTable(tableModel);
								table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
								pnlUpper.add(new JScrollPane(table), BorderLayout.CENTER);
							}
						}
						pnlCenter.add(pnlUpper);
					}
					{
						JPanel pnlBottom = new JPanel();
						pnlBottom.setLayout(new GridLayout(5, 2));
						pnlBottom.setBorder(BorderFactory.createTitledBorder("Bottom Panel"));
						{
							{
								pnlBottom.add(WidgetFactory.createLabel("First Name"));
								txtFirstName = WidgetFactory.createTextField();
								pnlBottom.add(txtFirstName);
							}
							{
								pnlBottom.add(WidgetFactory.createLabel("Last Name"));
								txtLastName = WidgetFactory.createTextField();
								pnlBottom.add(txtLastName);
							}
							{
								pnlBottom.add(WidgetFactory.createLabel("Hire Date"));
								txtHireDate = WidgetFactory.createTextField();
								pnlBottom.add(txtHireDate);
							}
							{
								pnlBottom.add(WidgetFactory.createLabel("Department"));
								comboBoxModel = new DepartmentComboBoxModel();
								cmbDepartments = WidgetFactory.createComboBox(comboBoxModel);
								pnlBottom.add(cmbDepartments);

							}
						}
						pnlCenter.add(pnlBottom);
					}
				}
			}
			frmMain.add(pnlCenter, BorderLayout.CENTER);
		}
	}

	public void setVisible(boolean visible) {
		frmMain.setVisible(visible);
	}

	public void setEmployees(List<Employee> employees) {
		tableModel.setEmployees(employees);
		tableModel.fireTableDataChanged();
	}

	public void setDepartments(List<Department> allDepartments) {
		for (Department department : allDepartments) {
			comboBoxModel.addElement(department);
		}
	}

	public void addTableSelectionListener(ListSelectionListener listener) {
		table.getSelectionModel().addListSelectionListener(listener);
	}

	public Employee getSelectedEmployee() {
		int index = table.getSelectedRow();
		return tableModel.getEmployees().get(index);
	}

	public void updateEmployeeValues(Employee employee) {
		txtFirstName.setText(employee.getFirstName());
		txtLastName.setText(employee.getLastName());
		txtHireDate.setText(employee.getHireDate().toString());
		cmbDepartments.setSelectedItem(employee.getDepartment());
	}

	public void gotoFirstRow() {
		table.setRowSelectionInterval(0, 0);
		updateEmployeeValues(tableModel.getEmployees().get(0));
	}
}
